<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="广告位名称" prop="name">
        <el-input placeholder="" v-model="dataForm.name" maxlength="16"></el-input>
      </el-form-item>
      <el-form-item label="广告位说明" prop="remark">
        <el-input type="textarea" v-model="dataForm.remark" maxlength="200"></el-input>
      </el-form-item>
      <!-- <el-form-item label="渠道" prop="owner_type">
        <el-select v-model="dataForm.owner_type" placeholder="请选择" @change="changeOwner_type">
          <el-option v-for="item in ownerType" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="广告位置" prop="mark_code">

        <el-select v-model="dataForm.mark_code" placeholder="请选择">
          <el-option v-for="item in bannerCode2" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="广告位类型" prop="ad_location_type">
        <el-select v-model="dataForm.ad_location_type" placeholder="请选择">
          <el-option v-for="item in adLocationType" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <div class="flex">
        <el-form-item label="宽高" prop="width">
          <!-- <el-input placeholder="" oninput="value=parseInt(value)" v-model="dataForm.width" type="number" style="width: 120px;"></el-input> -->
          <el-input-number v-model="dataForm.width" :precision="0" controls-position="right" :min="0"></el-input-number>
        </el-form-item>

        <span class="mlr20" style="margin-bottom:20px">x</span>
        <el-form-item prop="height" label-width="0px">
          <!-- <el-input v-model="dataForm.height" style="width: 120px;" type="number"></el-input> -->
          <el-input-number v-model="dataForm.height" :precision="0" controls-position="right" :min="0"></el-input-number>
        </el-form-item>

      </div>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from '@/utils'
import Imglist from './../../oss/oss-uploadImg'
import seviceApi from '@/api/serviceApi.js'
// 广告位渠道和标识码联动唯一标识
export default {
  components: {
    Imglist
  },
  data () {
    return {
      visible: false,
      dataForm: {
        id: '0',
        name: '',
        remark: '', // 图片
        mark_code: '', // 广告位
        ad_location_type: '', // 广告类型
        width: '',
        height: '',
        owner_type: 0
      },
      bannerCode: [],
      bannerCode2: [
        {
          label: 'APP首页Banner',
          value: 'home-banner',
          type: 0
        },
        {
          label: '创业专区Banner',
          value: 'entrepreneurshipzone-banner',
          type: 0
        },
        {
          label: '公益商城首页Banner',
          value: 'publicshop-banner',
          type: 0
        },
        {
          label: '关于绿马助残公益基金Banner',
          value: 'publicwelfarezone-banner',
          type: 0
        },
        {
          label: '公益讲堂Banner',
          value: 'forum-banner',
          type: 0
        },
        {
          label: '同城商家Banner',
          value: 'localMerchant-banner',
          type: 0
        },
        {
          label: 'app启动广告',
          value: 'startAdvertising',
          type: 0
        },
        {
          label: '资讯轮播图',
          value: 'news-swiper',
          type: 0
        },
        {
          label: '资讯banner图',
          value: 'news-banner',
          type: 0
        }
      ],
      adLocationType: [
        {
          label: '普通广告位',
          value: 0
        }, {
          label: '幻灯广告位',
          value: 1
        }
      ],
      ownerType: [
        {
          label: '平台广告位',
          value: 0
        },
        {
          label: '商家店铺广告位',
          value: 1
        }

      ],
      dataRule: {
        name: [
          { required: true, message: '广告位名称不能为空', trigger: 'blur' }
        ],
        remark: [
          { required: true, message: '广告位说明不能为空', trigger: 'blur' }
        ],
        mark_code: [
          { required: true, message: '广告位标识码不能为空', trigger: 'blur' }
        ],
        ad_location_type: [
          { required: true, message: '请选择广告位类型', trigger: 'blur' }
        ],
        width: [
          { required: true, message: '宽不能为空', trigger: 'blur' }
        ],
        height: [
          { required: true, message: '高不能为空', trigger: 'blur' }
        ],
        owner_type: [
          { required: true, message: '请选择渠道', trigger: 'blur' }
        ]

      }
    }
  },

  methods: {
    changeOwner_type () {
      // this.bannerCode2 = this.bannerCode.filter(val => val.type == this.dataForm.owner_type)
      // this.dataForm.mark_code=this.bannerCode[this.dataForm.owner_type].value
    },
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
      })
      if (id) {
        seviceApi.getAdLocationInfo(id).then(res => {
          if (res && res.data.code == 200) {
            this.dataForm = res.data.data
            this.changeOwner_type()
          }
        })
      }
    },
    // 提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.dataForm.id == 0) {
            seviceApi.saveAdLocation(this.dataForm).then(res => {
              if (res && res.data.code == 200) {
                this.$message({
                  message: '添加成功',
                  type: 'success'
                })
                this.visible = false
                this.$emit('updataList', 1)
              }
            })
          } else {
            seviceApi.updateAdLocation(this.dataForm).then(res => {
              if (res && res.data.code == 200) {
                this.$message({
                  message: '修改成功',
                  type: 'success'
                })
                this.visible = false
                this.$emit('updataList', 1)
              }
            })
          }
        }
      })
    }
  }
}
</script>

<style lang="scss">
.flex {
  display: flex;
}

.mlr20 {
  margin: 0px 10px;
}

.line-height36 {
  line-height: 36px;
}
</style>
